package com.dense.kuiniu.bookborrow.xjSetting.view.other;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.xjSetting.listener.XJOnClickListener;
import com.dense.kuiniu.bookborrow.xjSetting.manager.YuyinUtils;

public class YuYinLayout extends LinearLayout {
    private String TAG = "YuYinLayout";

    public YuYinLayout(Context context) {
        this(context, null);
    }

    public YuYinLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YuYinLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private ImageView yuyin_switch;

    private TextView voice_title;
    private EditText voice_connect;


    private TextView save;

    private boolean isPlay;

    private AudioManager audio;
    private int maxVoice;
    private int curVoice;


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.yuyin_layout, this);

        audio = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
        maxVoice = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        yuyin_switch = findViewById(R.id.yuyin_switch);

        voice_title = findViewById(R.id.voice_title);
        voice_connect = findViewById(R.id.voice_connect);
        save = findViewById(R.id.yuyin_save);
        yuyin_switch.setOnClickListener(onClickListener);
        save.setOnClickListener(onClickListener);

        isPlay = YuyinUtils.getInstall().getYuyinPlay();
        setYuyinPlayView();

        initVoice();
    }

    private void initVoice() {
        curVoice = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
        voice_title.setText(String.format("音量设置（最大%d,当前%d）", maxVoice, curVoice));
        voice_connect.setText(String.valueOf(curVoice));
    }

    private void setYuyinPlayView() {
        yuyin_switch.setImageResource(isPlay ? R.mipmap.open_yuyin : R.mipmap.close_yuyin);
    }


    private XJOnClickListener onClickListener = new XJOnClickListener() {
        @Override
        public void onclickView(View view) {
            switch (view.getId()) {
                case R.id.yuyin_switch:
                    isPlay = !isPlay;
                    setYuyinPlayView();
                    break;
                case R.id.yuyin_save:
                    String voiceValue = voice_connect.getText().toString();
                    if (StringUtils.isEmpty(voiceValue)) {
                        ToastUtils.showShort("输入不能为空");
                        return;
                    }
                    try {
                        int vv = Integer.parseInt(voiceValue);
                        if (vv >= 0 && vv <= maxVoice) {
                            if (vv!=curVoice) {
                                audio.setStreamVolume(AudioManager.STREAM_MUSIC, vv, 1);
                                initVoice();
                            }
                        } else {
                            ToastUtils.showShort("音量值请输入请输入0-" + maxVoice + "之间的数字");
                            return;
                        }
                    } catch (Exception e) {
                        ToastUtils.showShort("音量只能是整数！！");
                        return;
                    }
                    YuyinUtils.getInstall().setYuyinPlay(isPlay);
                    ToastUtils.showShort("保存成功");
                    break;
            }
        }
    };



}

